﻿<?php
session_start();
require_once("../Structure/fonctions.php");

$PAGE_TITLE = "Rédiger un article";
$CONTENT_INCLUDE = "write_article.html";
//------------------------------------------------------------------//
//      Envoi de l'article                                          //
//------------------------------------------------------------------//

if(isset($_POST['valider'])){

	$bdd = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_BASE);
	
	include("../Structure/updateUser.php");
	
	$autorID = $_SESSION['user_id'];
	$title =  $_POST['title'];
	$imageID = 1;
	$content = $_POST['contentEditor'];
	$type = $_POST['type'];
	$isComsAlwd = 0;
	$statusID = 0;

	if(isset($_POST['comment_allowed'])) {
		if($_POST['comment_allowed'] == 'True'){$isComsAlwd = 1;}
	}
	if($user['roleID'] == 3){$statusID = 1;}
	
	$result = "";
	///Analyse le contenu de l'article et stocke les liens de toutes les images (REGEX)
	preg_match_all('#<img .*src=(?:"|\')(.+)(?:"|\').*>#Uis', $content, $result);
	
	///Insère les images en base
	$i = 0;
	foreach($result as $value)
	{
		if($i != 0)
		{
			foreach($value as $link)
			{
				$query = "INSERT INTO images(autorID, link, statusID)
				VALUES('".$autorID."', '".$link."', 1)";
				$res = mysqli_query($bdd, $query) or die (mysqli_error($bdd));
			}
		}
		$i++;
	}
	
	
	//Insère l'article dans la base
	$query = "INSERT INTO articles(autorID, imageID, title, content, type, statusID, note, date, commentAllowed) 
	VALUES('".$autorID."', '".$imageID."', '".$title."', '".$content."', '".$type."', '".$statusID."' , 0 , SYSDATE(), '".$isComsAlwd."')";
	
	$res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
	
	//Récupération de l'id de l'article que l'on vient d'insérer
	$query = "SELECT id FROM articles WHERE id = LAST_INSERT_ID()";
	$res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
	
	$row = mysqli_fetch_array($res);
	
	$articleID = $row['id'];
	
	///On analyse le fichier de news envoyé
	if ($_FILES["articleImg"]["error"] == 0)
	{
		if ($_FILES["articleImg"]["size"] < 5242880)///Taille inférieur a 5Mio
		{
			$extensions_autorisees = array( '.jpg' , '.jpeg' , '.gif' , '.png' , '.bmp' );
            $extension_upload = strtolower(strrchr($_FILES["articleImg"]["name"], '.'));
			if (in_array($extension_upload, $extensions_autorisees))//On vérifie l'extension
			{
				//déplace le fichier uploadé
				$move = move_uploaded_file($_FILES["articleImg"]["tmp_name"], LOCALROOT."img/article/".$articleID.$extension_upload);
				//On l'insere en base puis on met à jour l'article avec l'id de l'image
				if ($move) {
                    $query = "INSERT INTO images (link, autorID, statusID)
                              VALUES      ('img/article/".$articleID.$extension_upload."', '".$_SESSION["user_id"]."', 1) ;";
                    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));

                    $query = "UPDATE  articles
                              SET     imageID = LAST_INSERT_ID()
                              WHERE   id = ".$articleID." ;";
                    $res = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
                }
			}
		}
	}
}

include_once ("../Structure/structure.php");
?>